#define _CRT_SECURE_NO_WARNINGS 1


#include "contact.h"

int CheckCapacity(Contact* pc);

void LoadContact(Contact* pc)
{
	FILE* pf = fopen("contact.dat", "rb");

	if (NULL == pf)
	{
		perror("LoadContact");
		return;
	}

	PeoInfo tmp = { 0 };

	while (fread(&tmp, sizeof(PeoInfo), 1, pf))
	{
		if (0 == CheckCapacity(pc))
		{
			perror("LoadContact");
			return;
		}

		pc->data[pc->sz] = tmp;
		pc->sz++;
	}

	fclose(pf);
	pf = NULL;
}

void InitContact(Contact* pc)
{
	assert(pc);

	pc->data = (PeoInfo*)malloc(DEFAULT_SZ * sizeof(PeoInfo));

	if (NULL == pc->data)
	{
		perror("InitContact");
		return;
	}

	pc->sz = 0;
	pc->capacity = DEFAULT_SZ;

	LoadContact(pc);
}


int CheckCapacity(Contact* pc)
{
	if (pc->sz == pc->capacity)
	{
		PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_SZ) * sizeof(PeoInfo));

		if (NULL == ptr)
		{
			perror("CheckCapacity");
			return 0;
		}
		else
		{
			pc->data = ptr;
			pc->capacity += INC_SZ;
			printf("增容成功\n");
			return 1;
		}
	}

	return 1;
}

void AddContact(Contact* pc)
{
	assert(pc);

	if (0 == CheckCapacity(pc))
	{
		return;
	}

	printf("请输入名字:>");
	scanf("%s", pc->data[pc->sz].name);
	printf("请输入性别:>");
	scanf("%s", pc->data[pc->sz].sex);
	printf("请输入年龄:>");
	scanf("%d", &(pc->data[pc->sz].age));
	printf("请输入电话:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("请输入地址:>");
	scanf("%s", pc->data[pc->sz].addr);

	pc->sz++;
	printf("成功增加联系人\n");
}

void ShowContact(const Contact* pc)
{
	assert(pc);

	int i = 0;
	printf("%-20s\t%-5s\t%-4s\t%-12s\t%-30s\n", "名字", "性别", "年龄", "电话", "地址");

	for (i = 0; i < pc->sz; i++)
	{
		printf("%-20s\t%-5s\t%-4d\t%-12s\t%-30s\n",
			pc->data[i].name,
			pc->data[i].sex,
			pc->data[i].age,
			pc->data[i].tele,
			pc->data[i].addr);
	}
}


static int FindByName(const Contact* pc, char name[])
{
	int i = 0;

	for (i = 0; i < pc->sz; i++)
	{
		if (0 == strcmp(pc->data[i].name, name))
		{
			return i;
		}
	}

	return -1;
}

void DelContact(Contact* pc)
{
	assert(pc);

	if (0 == pc->sz)
	{
		printf("通讯录为空，无法删除\n");
		return;
	}

	char name[MAX_NAME] = { 0 };

	printf("请输入要删除的人的名字:>");
	scanf("%s", name);
	int del = FindByName(pc, name);

	if (-1 == del)
	{
		printf("要删除的人不存在\n");
		return;
	}

	int i = 0;

	for (i = del; i < pc->sz - 1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}

	pc->sz--;
	printf("成功删除联系人\n");
}

void SearchContact(const Contact* pc)
{
	assert(pc);

	char name[MAX_NAME] = { 0 };
	printf("请输入要查找人的名字:>");
	scanf("%s", name);

	int pos = FindByName(pc, name);

	if (-1 == pos)
	{
		printf("要查找的人不存在\n");
		return;
	}

	printf("%-20s\t%-5s\t%-4s\t%-12s\t%-30s\n", "名字", "性别", "年龄", "电话", "地址");
	printf("%-20s\t%-5s\t%-4d\t%-12s\t%-30s\n",
		pc->data[pos].name,
		pc->data[pos].sex,
		pc->data[pos].age,
		pc->data[pos].tele,
		pc->data[pos].addr);
}

void ModifyContact(Contact* pc)
{
	assert(pc);

	char name[MAX_NAME] = { 0 };
	printf("请输入要修改人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);

	if (-1 == pos)
	{
		printf("要修改的人不存在\n");
		return;
	}

	printf("请输入名字:>");
	scanf("%s", pc->data[pos].name);
	printf("请输入性别:>");
	scanf("%s", pc->data[pos].sex);
	printf("请输入年龄:>");
	scanf("%d", &(pc->data[pos].age));
	printf("请输入电话:>");
	scanf("%s", pc->data[pos].tele);
	printf("请输入地址:>");
	scanf("%s", pc->data[pos].addr);
	printf("修改联系人成功\n");
}



void select()
{
	printf("********************************\n");
	printf("***** 1. name    2. age    *****\n");
	printf("********************************\n");
}

int cmp_by_name(const void* p1, const void* p2)
{
	return strcmp(((PeoInfo*)p1)->name, ((PeoInfo*)p2)->name);
}

int cmp_by_age(const void* p1, const void* p2)
{
	return ((PeoInfo*)p1)->age - ((PeoInfo*)p2)->age;
}

void SortContact(Contact* pc)
{
	assert(pc);

	if (0 == pc->sz)
	{
		printf("通讯录为空，无法排序\n");
		return;
	}

	int input = 0;

	do
	{
		select();
		printf("请选择按何种方式进行排序:>");
		scanf("%d", &input);

		switch (input)
		{
		case NAME:
			qsort(pc->data, pc->sz, sizeof(pc->data[0]), cmp_by_name);
			printf("排序成功\n");
			break;
		case AGE:
			qsort(pc->data, pc->sz, sizeof(pc->data[0]), cmp_by_age);
			printf("排序成功\n");
			break;
		default:
			printf("选择错误，请重新选择\n");
			break;
		}

	} while (input != NAME && input != AGE);

}

void DestroyContact(Contact* pc)
{
	free(pc->data);
	pc->data = NULL;
	pc->capacity = 0;
	pc->sz = 0;
}


void SaveContact(Contact* pc)
{
	FILE* pf = fopen("contact.dat", "wb");

	if (NULL == pf)
	{
		perror("SaveContact");
		return;
	}

	int i = 0;

	for (i = 0; i < pc->sz; i++)
	{
		fwrite(pc->data + i, sizeof(PeoInfo), 1, pf);
	}

	fclose(pf);
	pf = NULL;
}